<?php
// +----------------------------------------------------------------------
// | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://tplay.pengyichen.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------

namespace app\api\controller;
use app\api\controller\Base;
use think\Config;
use think\Session;
use think\Cookie;
use think\Db;
use think\Cache;
use \think\Validate;

class Common extends Base
{
    public function login()
    {
        $post = $this->param;
        $validate = new \think\Validate([
            'username|用户名'  => 'require',
            'password|密码' => 'require',
            //'captcha|验证码'=>'require|captcha'
        ]);
        if (!$validate->check($post)) {
            return $this->show(0,$validate->getError());
        }
        $user = Db::name('user')->where(['phone'=>$post['phone']])->find();

        if(!empty($user)) {
            if($user['password'] !== md5(md5($post['password']).$user['salt'])){
              return $this->show(0,'手机号或密码错误');
            }
            if($user['status'] == -1) {
              return $this->show(0,'你已被拉黑');
            }
            if($user['status'] == 0) {
              return $this->show(0,'你的账户倍禁止登录，请联系管理员');
            }

            $login_salt = salt(30);
            if(false == Db::name('user')->where(['id'=>$user['id']])->update(['login_num'=>$user['login_num']+1,'last_login_time'=>time(),'login_string'=>$login_salt])){
              return $this->show(0,'登录失败');
            }
        } else {
            return $this->show(0,'用户名不存在');
        }
        return $this->show(1,'登录成功',$this->setSign($login_salt));
    }

    public function registered()
    {
        $post = $this->param;
        $validate = new \think\Validate([
            'username|用户名'  => 'require',
            'password|密码' => 'require|confirm',
            'name|昵称' => 'require',
        ]);
        if (!$validate->check($post)) {
            return $this->show(0,$validate->getError());
        }
        if(!empty(Db::name('user')->where(['username'=>$post['username']])->find())) {
            return $this->show(0,'手机号已注册');
        }
        $post['status'] = 1;
        $post['salt'] = salt();
        $post['password'] = md5(md5($post['password']).$post['salt']);

        Db::startTrans();
        try{
            \think\loader::model('common/user')->allowField(true)->save($post);
            // 提交事务
            Db::commit();    
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return $this->show(0,$e->getMessage());
        }
        return $this->show(1,'注册成功');
    }

    public function logout()
    {
        if(!Cache::get($this->param['login_salt'])){
            return $this->show(0,'非法请求');
        }
        Cache::rm($this->param['login_salt']);
        return $this->show(1,'退出成功');
    }
}
